package com.dragonboat.service;
import com.dragonboat.util.PasswordUtil;
import com.dragonboat.entity.Event;
import com.dragonboat.entity.Registration;
import com.dragonboat.entity.Team;
import com.dragonboat.repository.EventRepository;
import com.dragonboat.repository.RegistrationRepository;
import com.dragonboat.repository.TeamRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class TeamService {
    @Autowired
    private TeamRepository teamRepository;

    @Autowired
    private EventRepository eventRepository;

    @Autowired
    private RegistrationRepository registrationRepository;

    public Optional<Team> login(Integer teamId, String password) {
        return teamRepository.findById(teamId)
                .filter(t -> PasswordUtil.matches(password, t.getPassword()));
    }
    public Optional<Team> loginByName(String teamName, String password) {
        return teamRepository.findByTeamName(teamName)
                .filter(t -> PasswordUtil.matches(password, t.getPassword()));
    }
    public List<Event> getAvailableEventsForTeam(Integer teamId) {
        List<Event> upcomingEvents = eventRepository.findByStatus("upcoming");
        upcomingEvents.forEach(event -> {
            Optional<Registration> registration = registrationRepository.findByEventEventIdAndTeamTeamId(event.getEventId(), teamId);
            event.setRegistered(registration.isPresent());
            registration.ifPresent(r -> event.setRegistrationStatus(r.getStatus()));
        });
        return upcomingEvents;
    }



    public Registration registerEvent(Integer eventId, Integer teamId) {
        Event event = eventRepository.findById(eventId)
                .orElseThrow(() -> new RuntimeException("赛事不存在"));

        Team team = teamRepository.findById(teamId)
                .orElseThrow(() -> new RuntimeException("队伍不存在"));

        if (registrationRepository.existsByEventEventIdAndTeamTeamId(eventId, teamId)) {
            throw new RuntimeException("您已经报名过该赛事");
        }

        Registration registration = new Registration();
        registration.setEvent(event);
        registration.setTeam(team);
        registration.setStatus("pending");
        return registrationRepository.save(registration);
    }

    public boolean cancelRegistrationByEvent(Integer eventId, Integer teamId) {
        Registration registration = registrationRepository
                .findByEventEventIdAndTeamTeamId(eventId, teamId)
                .orElseThrow(() -> new RuntimeException("未找到报名记录"));

        if ("approved".equals(registration.getStatus())) {
            throw new RuntimeException("已审核通过的报名不能取消");
        }

        registrationRepository.delete(registration);
        return true;
    }

    public List<Registration> getRegistrationsByTeam(Integer teamId) {
        return registrationRepository.findByTeamTeamId(teamId);
    }
// TeamService.java 添加以下内容

    public List<Event> getAllEvents() {
        return eventRepository.findByStatus("upcoming");
    }
    public Team updateTeamInfo(Integer teamId, String name, String newPassword) {
        return teamRepository.findById(teamId).map(team -> {
            if (name != null && !name.isEmpty()) team.setTeamName(name);
            if (newPassword != null && !newPassword.isEmpty()) team.setPassword(newPassword);
            return teamRepository.save(team);
        }).orElse(null);
    }

    public Team getTeamById(Integer teamId) {
        return teamRepository.findById(teamId).orElse(null);
    }
}